<?php

namespace App\Models;

Class Group extends BaseModel
{

    protected $table = 'group';

    public function getOne($where, $fileds = '*', $order = '')
    {
        if (!$order) {
            $order = ['id' => 'desc'];
        }
        return $this->multiSelect($fileds)->multiWhere($where)->multiOrder($order)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->_add($data);
    }


    /***
     * @param array $id
     * @param array $data
     * 更新数据
     */
    public function up($where, $data)
    {
        return $this->_updata($where, $data);
    }

    /**
     * @param $id
     * 删除数据
     */
    public function del($where)
    {
        return $this->_del($where);
    }

    /**
     * 判断是否创建团队
     */
    public function isCreateGroup($mid)
    {
        $res = $this->getOne(['member_id' => $mid]);
        if ($res) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否加入了团队
     */
    public function isJoinGroup($id)
    {
        $where['team_pools'] = array('like', "%" . $id . "%");
        $res = $this->getOne($where);
        if ($res) {
            return true;
        }
        return false;
    }

    /**
     *  得到我的团队信息
     */
    public function getGroupInfo()
    {
        $is_createGroup = $this->isCreateGroup(session('uid'));
        $is_joinGroup = $this->isJoinGroup(session('uid'));
        $groupinfo = [
            'isCreateGroup' => $is_createGroup,
            'is_JoinGroup' => $is_joinGroup,
        ];
        return $groupinfo;
    }

    /**
     * 团队列表
     */
    public function groupList()
    {
        $res = $this->getOne(array('member_id' => session('uid')));
        return $res;
    }

    /**
     * 后台团队列表
     */
    public function adminGroupList($request)
    {
        $keyword = $request['keyword'] ? $request['keyword'] : '';
        $where = [];
        if ($keyword) {
            $where['group_name'] = ['like','%'.$keyword.'%'];
        }
        $start = date("Y-m-d",strtotime($request['starttime']));
        $end = date("Y-m-d",strtotime($request['endtime']));
        $start1 = $start;
        $end1 = $end;
        if($start>$end){
            $start1 = $end;
            $end1 = $start;
        }
        if($request['starttime']&&$request['endtime']){
            $where['updated_at'] = ['glt',[['>',$start1],['<=',$end1]]];;
        }

        $order['id'] = 'desc';
        $data = $this->getList($where, '*', $order, 10);
        //dd($data);
        if ($data) {
            $data = $data->toArray();
            $data['pageNoList'] = $this->getPageNoList($data['last_page'], request('page', 1), 10);
        }
        return $data;
    }

    /**
     * 加入团队
     */
    public function joinGroup($request)
    {
        $mid = session('uid');
        $name = $request['name'];
        $pass = $request['password'];
        if (empty($mid)) {
            return ['state' => 400, 'msg' => "参数错误"];
        }
        // 判断该用户是否加入任何团队
        if ($this->isJoinGroup(session('uid'))) {
            return ['state' => 400, 'msg' => "已经进入了某个团队"];
        }

        // 判断该团队是否存在
        $res = $this->getOne(array('group_name' => $name));
        if (!$res) {
            return ['state' => 400, 'msg' => "该团队不存在"];
        }

        // 验证口令是否正确
        if ($pass != $res['password']) {
            return ['state' => 400, 'msg' => "进团口令错误"];
        }

        // 处理加入团队
        $teampools = json_decode($res['team_pools']);
        $teampools[] = $mid;
        $res = $this->up(array('id' => $res['id']), ['team_pools' => json_encode($teampools)]);
        if ($res) {
            return ['state' => 200, 'msg' => "进团成功"];
            $this->up(array('id' => $res['id'], ['num' => $res['num'] + 1])); // 更新数量
        }
        return ['state' => 400, 'msg' => "进团失败，请重试"];
    }

    /**
     * 我加入的团队
     */
    public function myJoinGroup()
    {
        $where['team_pools'] = array('like', "%" . session('uid') . "%");
        return $this->getOne($where);
    }

    /**
     * 新增团队
     */
    public function createGroup($request)
    {

        $name = $request['name'];
        $pass = $request['password'];
        // 判断提交的内容是否为空
        if (empty($name) || empty($pass))
            return ['state' => 400, 'msg' => "提交内容不能为空"];
        // 判断该团队名称是否存在
        $res = $this->getOne(array('group_name' => $name));
        if ($res['password'] == $pass) {
            return ['state' => 400, 'msg' => "该团队名已存在"];
        }

        // 判断是否已创建团队
        if ($this->isCreateGroup(session('uid'))) {
            return ['state' => 400, 'msg' => "已经创建了团队"];
        }

        $inser['group_name'] = htmlspecialchars($name);
        $inser['password'] = htmlspecialchars($pass);
        $inser['member_id'] = intval(session('uid'));
        $inser['num'] = 0;
        $inser['team_pools'] = '';
        $inser['created_at'] = date("Y-m-d H:i:s", time());
        $inser['updated_at'] = date("Y-m-d H:i:s", time());
        $result = $this->add($inser);
        if ($result) {
            return ['state' => 200, 'msg' => "团队创建成功"];
        }
        return ['state' => 400, 'msg' => "团队创建失败"];
    }
}